<?php
//公共分页
function MyPage($total,$num=10,$act='',$config=''){
	$p = new Page($total, $num);
	$p->action = $act;
	$p->setConfig('header', '条数据'); 
	if(!empty($config)){
		foreach($config as $k=>$v){
			$p->setConfig($k, $v); 
		}
	}
	//$p->setConfig('prev', "<"); 
	//$p->setConfig('next', '>'); 
	//$p->setConfig('first', '<<'); 
	//$p->setConfig('last', '>>'); 
	$page = $p->show();            //分页的导航条的输出变量	 
	$res = array(
				"firstRow"=>$p->firstRow,
				"listRows"=>$p->listRows,
				"page"=>$page
		   );	
	return $res;   
}

function indexFun(){
	$PATH_INFO = $_SERVER['REQUEST_URI'];
	if($PATH_INFO=='/' || $PATH_INFO=='' || $PATH_INFO == NULL){
		return;
	}else{
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: /");  
		exit();
	}	
}

//404跳转
function My404(){
	header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
	import('@.Class.mcache');
	$inc = MCache::get('Setting/inc');
	return $inc['index_url'];	
}

//输出json
function josn_set($str,$utf8=false){
   $json = json_encode($str);
   if($utf8){
	   $search = "#\\\u([0-9a-f]+)#ie"; 
	   $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))"; 
	   $json = @preg_replace($search, $replace, $json);
   }
   return $json; 
}

//输出
function Output($code=1,$arr="",$msg="",$page=""){
	switch($code){
		case 0:
		   	 $msg = 'server_error';	
		   break;
		case 2:
		   	 $msg = 'empty_fun';
		   break;
		case 3://自定义
			 $code = 0;
			 if($msg==''){
				$msg = "操作失败"; 
			 }	
		   break;        
		default:
	  		 $msg = $msg;
			 
	}	
	
	$res['data'] = empty($arr) ? array() : $arr;
	
	if($code==1){
		$res["code"] = 200;	
	}elseif($code==2){
		$res["code"] = 404;	
	}else{
		$res["code"] = 200;
		$res['msg'] = $msg;		
	}
	if($_GET['debug']==1){		
		var_dump($res);	
	}elseif($_GET['debug']==2){
		print_r($res);	
	}else{
		echo josn_set($res,true);
	}
	exit();
}

function getMyUrl($name){
	preg_match("/[\w\-]+\.\w+$/", $_SERVER['HTTP_HOST'], $httpUrl);
	return "http://".$name.".".$httpUrl[0];	
}

function formatDate($time){  
	$rtime = date ( "m-d H:i", $time );  
	$htime = date ( "H:i", $time );  
	$time = time () - $time;
	if ($time < 60) {  
		$str = '刚刚';  
	} elseif ($time < 60 * 60) {  
		$min = floor ( $time / 60 );  
		$str = $min . '分钟前';  
	} elseif ($time < 60 * 60 * 24) {  
		$h = floor ( $time / (60 * 60) );  
		$str = $h . '小时前 ' . $htime;  
	} elseif ($time < 60 * 60 * 24 * 3) {  
		$d = floor ( $time / (60 * 60 * 24) );  
		if ($d == 1)  
			$str = '昨天';  
		else  
			$str = '前天';  
	} else {  
		$str = $rtime;  
	}  
	return $str;    
}

/**
 * 发送HTTP请求方法
 * @param  string $url    请求URL
 * @param  array  $params 请求参数
 * @param  string $method 请求方法GET/POST
 * @return array  $data   响应数据
 */
function http($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
	
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }

    /* 初始化并执行curl请求 */
    $ch = curl_init();
	dump($url);
	curl_setopt($ch, CURLOPT_URL, $url); //设置选项,包括URL
    //curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
	dump($data);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}